package com;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Statement作用 1.执行DDL、DML语句
 *                2.执行DQL语句
 *
 */

public class JDBCDemo04_Statement {
   @Test
   //执行DML语句
   public void test01() throws Exception {
      //1.注册驱动
      //Class.forName("com.mysql.jdbc.Driver");
      //2.获取连接
      String url="jdbc:mysql://127.0.0.1:3306/db01";
      String user="root";
      String password="137867";
      Connection connection = DriverManager.getConnection(url,user,password);
      //3编写sql
      String sql="update tb01 set number=3000 where id=1";
      //4.执行sql对象
      Statement statement = connection.createStatement();
      //5.执行sql
      int i = statement.executeUpdate(sql);
      //6处理结果
      System.out.println(i);
      //7关闭资源
      statement.close();
      connection.close();
   }

   @Test
   //执行DDL语句 成功也可能返回0
   public void test02() throws Exception {
      //1.注册驱动
      //Class.forName("com.mysql.jdbc.Driver");
      //2.获取连接
      String url="jdbc:mysql://127.0.0.1:3306/db01";
      String user="root";
      String password="137867";
      Connection connection = DriverManager.getConnection(url,user,password);
      //3编写sql
      String sql="CREATE DATABASE db2";
      //4.执行sql对象
      Statement statement = connection.createStatement();
      //5.执行sql
      int i = statement.executeUpdate(sql);
      //6处理结果
      System.out.println(i);
      //7关闭资源
      statement.close();
      connection.close();
   }
}
